package cms.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import cms.business.CMSBusiness;
import cms.hibernate.po.Category;

import com.opensymphony.xwork2.ActionSupport;

public class CategoryAction extends ActionSupport {
	
	private CMSBusiness cMSBusiness = new CMSBusiness();
	private Category category;
	private List categories ;	
	public String delete() {
		boolean flag = this.cMSBusiness.deleteCategoryById(this.category.getId());
		if(!flag) {
			//throw new RuntimeException("error in delete category!");
			addActionError(getText("DELETE_FAILED"));
		}
		else
		{
			addActionMessage(getText("DELETE_SUCCESS"));
		}
		list();
		return "afterdelete";
	}
	/*
	public String listForAjax() throws IOException {
		String text = "";
		
		List list = this.cMSBusiness.getCategoryList(this.category.getParentId());
		for(Object o : list) {
			Category c = (Category)o;
			text += "<category><name>"+c.getCategoryName()+"</name>" +
					"<id>"+c.getId()+"</id></category>";
		}
		
		text = "<categories>"+text+"</categories>";
		
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		out.println(text);
		out.flush();
		
		return null;
	}
	*/
	@SuppressWarnings("unchecked")
	public String list() {
		//System.out.println("id:" + this.category.getParentId());
		int queryId=-1;
		if(category!=null&&category.getParentId()!=null&&category.getParentId()>0)
			queryId=category.getParentId();
		else
		{
			category=new Category();
			category.setParentId(queryId);
		}
		categories = new ArrayList();		
		this.categories.addAll(this.cMSBusiness.getCategoryList(queryId));
		
		return "list";
	}
	public String saveedit()
	{
		categories = new ArrayList();		
		this.categories.addAll(this.cMSBusiness.getCategoryList(-1));
		if(category==null)
		{
			addActionError(getText("SAVE_FAILED"));
			return "save";
		}
		if(category.getCategoryName()==null||category.getCategoryName().equals(""))
		{
			addActionError(getText("CATEGORY_NAMEBLANK"));
			return "save";
		}
		if(this.cMSBusiness.SaveCategory(category)==true)
		{
			addActionMessage(getText("SAVE_SUCCESS"));			
		}
		else
		{
			addActionError(getText("SAVE_FAILED"));
		}
		return "save";
	}
	/*
	 * 进入新建或者编辑分类页面
	 */
	public String edit() {
		//this.category = this.cMSBusiness.getCategoryById(1);
		categories = new ArrayList();		
		this.categories.addAll(this.cMSBusiness.getCategoryList(-1));
		if(category==null){
			category=new Category();
			category.setParentId(-1);
		}
		else if(category.getId()!=null)
		{
			category=cMSBusiness.getCategoryById(category.getId());
			
		}
		
		return "edit";
	}
	
	public void setCategories(List categories) {
		this.categories = categories;
	}

	public List getCategories() {
		return categories;
	}
	
	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	
	
}
